/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package ui.windows.forms.searchResult;

import constants.UiConstants;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Class for handling key events occuring on SearchResultForm
 *
 * @author rymek
 *         User: Rymek
 *         Date: 29.08.11
 *         Time: 23:03
 */
class SearchResultFormKeyAdapter extends KeyAdapter {

    private final SearchResultForm searchResultForm;        // parent form

    /**
     * Constructor.
     * Sets reference to parent form.
     *
     * @param searchResultForm parent SearchResultForm
     */
    public SearchResultFormKeyAdapter(SearchResultForm searchResultForm) {
        this.searchResultForm = searchResultForm;
    }

    /**
     * Performs actions depending on which component activated them
     * on SearchResultForm
     *
     * @param e Event
     */
    @Override
    public void keyPressed(KeyEvent e) {
        super.keyPressed(e);

        if (e.getSource() == this.searchResultForm.getRequestBody() && e.isControlDown() && e.getKeyCode() == KeyEvent.VK_F)
            this.searchThroughRequest();

        if (e.getSource() == this.searchResultForm.getResponseBody() && e.isControlDown() && e.getKeyCode() == KeyEvent.VK_F)
            this.searchThroughResponse();

        if (e.getSource() == this.searchResultForm.getSearchString() && e.getKeyCode() == KeyEvent.VK_ENTER)
            this.find();

        if (e.getSource() == this.searchResultForm.getSearchString() && e.getKeyCode() == KeyEvent.VK_ESCAPE)
            this.searchResultForm.resetFindIn();
    }

    /**
     * Performs search through request
     */
    private void searchThroughRequest() {
        this.searchResultForm.resetFindIn();
        this.searchResultForm.getFindInLabel().setText(UiConstants.LBL_SRCH_REQUEST);
        this.searchResultForm.getSearchPanel().setVisible(true);
        this.searchResultForm.getResultForm().repaint();
        this.searchResultForm.setSearchSource(this.searchResultForm.getRequestBody());
        this.searchResultForm.getSearchString().requestFocus();
    }

    /**
     * Performs search through response
     */
    private void searchThroughResponse() {
        this.searchResultForm.resetFindIn();
        this.searchResultForm.getFindInLabel().setText(UiConstants.LBL_SRCH_RESPONSE);
        this.searchResultForm.getSearchPanel().setVisible(true);
        this.searchResultForm.getResultForm().repaint();
        this.searchResultForm.setSearchSource(this.searchResultForm.getResponseBody());
        this.searchResultForm.getSearchString().requestFocus();
    }

    /**
     * Fires up click action on find button
     */
    private void find() {
        this.searchResultForm.getFindIn().doClick();
    }
}
